#!/bin/bash
#
# MONPROGRAMME          Start/Stop the MONPROGRAMME daemon.
#
# chkconfig: 2345 90 60
# description:MONPROGRAMME transfer log file to a remote syslog
# processname: MONPROGRAMME
# config: /etc/MONPROGRAMME.conf
# pidfile: /var/run/MONPROGRAMME.pid

# Source function library.
. /etc/init.d/functions

PIDSPATH=/var/run
SRVNAME=MONPROGRAMME

execdir="/opt/applis/pj-poi-inject"
execprog="/opt/ciweb2g/products/jrockit-R27.2.0-jdk1.5.0_10/bin/java -jar pj-poi-inject.jar"
lockfile=/var/lock/subsys/$SRVNAME

start() {
        RETVAL=1
        echo -n "Starting $SRVNAME : "
        if [ ! -f "$PIDSPATH/$SRVNAME.pid" ]; then
                cd $execdir
                $execprog >/dev/null 2>&1 &
                RETVAL=$?
                PROCPID=$!
                echo "$PROCPID" >> $PIDSPATH/$SRVNAME.pid
        else
                echo -n " Already Running "
        fi
        
        if [ $RETVAL -eq 0 ]; then
                touch $lockfile
                success
        else
                failure
        fi
        echo 
        return $RETVAL
}
        
stop() {
        echo -n $"Shutting down $SRVNAME: "
        RETVAL=1
        if [ -f "$PIDSPATH/$SRVNAME.pid" ]; then
                cat "$PIDSPATH/$SRVNAME.pid" | xargs -n1 kill -9
                RETVAL=$?
                rm -f "$PIDSPATH/$SRVNAME.pid"
                [ $RETVAL -eq 0 ] && rm -f $lockfile
                success
        fi
        echo
        
        return $RETVAL
}

status() {
        STATUS=0
        [ -f "$PIDSPATH/$SRVNAME.pid" ] && STATUS=$(ps -edf | grep $SRVNAME | awk '{print $2}' | grep -c `cat $PIDSPATH/$SRVNAME.pid`)

        echo -n "STATUS de $SRVNAME : ["
        if [ "$STATUS" -eq "1" ]; then
                echo -ne '\E[30;47mRUNNING'
        else
                echo -ne '\E[31;47mSTOPPED'
        fi
        tput sgr0
        echo "]"
}
# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        RETVAL=$?
        ;;
  restart|reload)
        stop
        start
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=3
esac

exit $RETVAL
